//
//  SYDBaseRequest.m
//  PinGJByOwnerSide
//
//  Created by lantaiyuan on 2017/8/19.
//  Copyright © 2017年 lantaiyuan. All rights reserved.
//

#import "SYDBaseRequest.h"

@implementation SYDBaseRequest

static SYDBaseRequest *_manager;
+ (instancetype)shareTool
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _manager = [SYDBaseRequest manager];
        _manager.requestSerializer = [AFJSONRequestSerializer serializer];
        _manager.responseSerializer = [AFJSONResponseSerializer serializer];
        // 设置超时时间
        _manager.requestSerializer.timeoutInterval = 15.0f;
        _manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", @"text/xml", @"text/plain", @"application/javascript", @"text/js", nil];
    });
    return _manager;
}

-(void)KPOST:(NSString *)URLString parameters:(id)parameters success:(void (^)(NSURLSessionDataTask *, id))success failure:(void (^)(NSURLSessionDataTask *, NSError *))failure{
    
    self.responseSerializer = [AFJSONResponseSerializer serializer];
    NSLog(@"-----链接:%@",URLString);
    NSLog(@"----请求参数:%@",[parameters mj_JSONString]);
    NSLog(@"%@%@",URLString,[parameters mj_JSONString]);
    [SVProgressHUD displayWithStatus:@""];
    
    [[UIApplication sharedApplication]setNetworkActivityIndicatorVisible:YES];
    [self POST:URLString parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [SVProgressHUD hide];
        [[UIApplication sharedApplication]setNetworkActivityIndicatorVisible:NO];
        NSLog(@"----返回结果:%@",[responseObject mj_JSONString]);
        if ([responseObject[@"statusCode"]integerValue] != 200) {
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [SVProgressHUD displayErrorWithStatus:responseObject[@"msg"]];
            });
        }
        success(task,responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [SVProgressHUD hide];
        [[UIApplication sharedApplication]setNetworkActivityIndicatorVisible:NO];
        NSLog(@"---error:%@",error.description);
        [SVProgressHUD displayErrorWithStatus:error.localizedDescription];
        failure(task,error);
    }];
}
-(void)KGET:(NSString *)URLString parameters:(id)parameters success:(void (^)(NSURLSessionDataTask *, id))success failure:(void (^)(NSURLSessionDataTask *, NSError *))failure{
    self.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSLog(@"-----链接:%@",URLString);
    NSLog(@"----请求参数:%@",[parameters mj_JSONString]);
    [SVProgressHUD displayWithStatus:@""];
    [self GET:URLString parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        id jsonDict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        NSLog(@"--------返回结果:%@",[jsonDict mj_JSONString]);
        success(task,jsonDict);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [SVProgressHUD hide];
        [SVProgressHUD displayErrorWithStatus:error.localizedDescription];
        NSLog(@"--error:%@",error);
        failure(task,error);
    }];
}
@end
